﻿
namespace Accounting.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Accounting.Web.Models;


    // Implements application logic using the AccountingDBEntities1 context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class ModelService : LinqToEntitiesDomainService<AccountingDBEntities1>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Goods' query.
        public IQueryable<Good> GetGoods()
        {
            foreach (Good good in ObjectContext.Goods)
            {
                ObjectContext.LoadProperty(good, "Warehouse");
            }
            return ObjectContext.Goods;
        }

        public void InsertGood(Good good)
        {
            if ((good.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(good, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Goods.AddObject(good);
            }
        }

        public void UpdateGood(Good currentGood)
        {
            this.ObjectContext.Goods.AttachAsModified(currentGood, this.ChangeSet.GetOriginal(currentGood));
        }

        public void DeleteGood(Good good)
        {
            if ((good.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Goods.Attach(good);
            }
            this.ObjectContext.Goods.DeleteObject(good);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Warehouses' query.
        public IQueryable<Warehouse> GetWarehouses()
        {
            return this.ObjectContext.Warehouses;
        }

        public void InsertWarehouse(Warehouse warehouse)
        {
            if ((warehouse.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(warehouse, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Warehouses.AddObject(warehouse);
            }
        }

        public void UpdateWarehouse(Warehouse currentWarehouse)
        {
            this.ObjectContext.Warehouses.AttachAsModified(currentWarehouse, this.ChangeSet.GetOriginal(currentWarehouse));
        }

        public void DeleteWarehouse(Warehouse warehouse)
        {
            if ((warehouse.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Warehouses.Attach(warehouse);
            }
            this.ObjectContext.Warehouses.DeleteObject(warehouse);
        }
    }
}


